<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>Vector constructors</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Vectors</a> &gt; <a href=
    "vector_constructors.html">Vector constructors</a>
  </div>

  <div class="name-format">
    Vector constructors
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;vector&gt;
  vector();
  vector( const vector&amp; c );
  vector( <strong>size_type</strong> num, const <a href=
"../containers.html">TYPE</a>&amp; val = <a href=
"../containers.html">TYPE</a>() );
  vector( <a href=
"../iterators.html">input_iterator</a> start, <a href=
"../iterators.html">input_iterator</a> end );
  ~vector();
</pre>

  <p>The default vector constructor takes no arguments, creates a new
  instance of that vector.</p>

  <p>The second constructor is a default copy constructor that can be
  used to create a new vector that is a copy of the given vector
  <em>c</em>.</p>

  <p>The third constructor creates a vector with space for <em>num</em>
  objects. If <em>val</em> is specified, each of those objects will be
  given that value. For example, the following code creates a vector
  consisting of five copies of the integer 42:</p>
  <pre class="example-code">
 vector&lt;int&gt; v1( 5, 42 );         
</pre>

  <p>The last constructor creates a vector that is initialized to
  contain the elements between <em>start</em> and <em>end</em>. For
  example:</p>
  <pre class="example-code">
 // create a vector of random integers
 cout &lt;&lt; &quot;original vector: &quot;;
 vector&lt;int&gt; v;
 for( int i = 0; i &lt; 10; i++ ) {
   int num = (int) rand() % 10;
   cout &lt;&lt; num &lt;&lt; &quot; &quot;;
   v.push_back( num );
 }
 cout &lt;&lt; endl;            

 // find the first element of v that is even
 vector&lt;int&gt;::iterator iter1 = v.begin();
 while( iter1 != v.end() &amp;&amp; *iter1 % 2 != 0 ) {
   iter1++;
 }              

 // find the last element of v that is even
 vector&lt;int&gt;::iterator iter2 = v.end();
 do {
   iter2--;
 } while( iter2 != v.begin() &amp;&amp; *iter2 % 2 != 0 );              

 // only proceed if we find both numbers
 if( iter1 != v.end() &amp;&amp; iter2 != v.begin() ) {
   cout &lt;&lt; &quot;first even number: &quot; &lt;&lt; *iter1 &lt;&lt; &quot;, last even number: &quot; &lt;&lt; *iter2 &lt;&lt; endl;         

   cout &lt;&lt; &quot;new vector: &quot;;
   vector&lt;int&gt; v2( iter1, iter2 );
   for( int i = 0; i &lt; v2.size(); i++ ) {
     cout &lt;&lt; v2[i] &lt;&lt; &quot; &quot;;
   }
   cout &lt;&lt; endl;
 }
</pre>

  <p>When run, this code displays the following output:</p>
  <pre class="example-code">
 original vector: 1 9 7 9 2 7 2 1 9 8
 first even number: 2, last even number: 8
 new vector: 2 7 2 1 9          
</pre>

  <p>All of these constructors run in <a href=
  "../complexity.html">linear time</a> except the first, which runs in
  <a href="../complexity.html">constant time</a>.</p>

  <p>The default destructor is called when the vector should be
  destroyed.</p>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
